package com.atguigu.flink.day05;

import com.atguigu.flink.beans.WaterSensor;
import com.atguigu.flink.func.WaterSensorMapFunction;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @author Felix
 * @date 2023/12/5
 * 该案例演示了窗口处理函数--process
 */
public class Flink04_window_process {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        SingleOutputStreamOperator<WaterSensor> wsDS = env
            .socketTextStream("hadoop102", 8888)
            .map(new WaterSensorMapFunction());

        //分组
        KeyedStream<WaterSensor, String> keyedDS = wsDS.keyBy(WaterSensor::getId);

        //开窗
        WindowedStream<WaterSensor, String, TimeWindow> windowDS
            = keyedDS.window(TumblingProcessingTimeWindows.of(Time.seconds(10)));

        //对窗口中的数据进行处理
        //apply 全量处理函数 --- 当窗口被触发计算，会对当前窗口的所有数据进行处理
        //      .apply(new WindowFunction-->apply)
        //                 apply(String s, TimeWindow window, Iterable<WaterSensor> input, Collector<String> out)
        //process 全量处理函数 --- 当窗口被触发计算，会对当前窗口的所有数据进行处理
        //      .process(new ProcessWindowFunction-->process)
        //                 process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out)
        // prcess更底层，方法中的context除了可以获取窗口对象外，还可以获取其它信息
        /*windowDS.apply(
            new WindowFunction<WaterSensor, String, String, TimeWindow>() {
                @Override
                public void apply(String s, TimeWindow window, Iterable<WaterSensor> input, Collector<String> out) throws Exception {
                    long count = input.spliterator().estimateSize();
                    String windowStart = DateFormatUtils.format(window.getStart(), "yyyy-MM-dd HH:mm:ss");
                    String windowEnd = DateFormatUtils.format(window.getEnd(), "yyyy-MM-dd HH:mm:ss");
                    out.collect("key=" + s + "的窗口[" + windowStart + "," + windowEnd + ")包含" + count + "条数据===>" + input.toString());
                }
            }
        ).print("$$");*/
        windowDS.process(
            new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                @Override
                public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                    long count = elements.spliterator().estimateSize();
                    String windowStart = DateFormatUtils.format(context.window().getStart(), "yyyy-MM-dd HH:mm:ss");
                    String windowEnd = DateFormatUtils.format(context.window().getEnd(), "yyyy-MM-dd HH:mm:ss");
                    out.collect("key=" + s + "的窗口[" + windowStart + "," + windowEnd + ")包含" + count + "条数据===>" + elements.toString());
                }
            }
        ).print("$$");
        env.execute();
    }
}
